package com.qf.test;

import org.apache.commons.pool2.impl.GenericObjectPoolConfig;
import org.junit.Test;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.Pipeline;

/**
 * @author Wy
 * @version V1.0
 * @Project MavenDemo
 * @Package com.qf.test
 * @Description:
 * @Date 2022/6/29 11:32
 */
public class Demo4 {
    @Test
    public void name() {
        //1. 创建连接池配置信息对象
        GenericObjectPoolConfig config = new GenericObjectPoolConfig();
        //最大连接数
        config.setMaxTotal(50);
        //最大空闲数
        config.setMaxIdle(10);
        //最小空闲数
        config.setMinIdle(5);
        //超时时间, 也就是多久没有用连接, 连接超时
        config.setMaxWaitMillis(3000);
        //2. 创建连接池
        JedisPool jedisPool = new JedisPool(config,"192.168.200.129",6379);
        //3. 通过连接池获取jedis对象
        Jedis resource = jedisPool.getResource();
        String key1="key2";
        String s = resource.get(key1);
        System.out.println(s);
        resource.close();
    }

    @Test
    public void get() {
        //1. 创建连接池
        JedisPool pool = new JedisPool("192.168.200.129",6379);
        //开始时间
       /* Jedis resource = pool.getResource();
        for (int i = 0; i < 100000; i++) {
            resource.incr("key2");
        }
        resource.close();*/
        //2. 获取jedis对象,
        Jedis resource = pool.getResource();
        //3. 创建管道
        Pipeline pipelined = resource.pipelined();
        for (int i = 0; i < 100000; i++) {
            pipelined.incr("key2");
        }
        pipelined.syncAndReturnAll();

        resource.close();
    }

}
